import { apiService } from "@service/index";

const userEndpoints = apiService
  .enhanceEndpoints({ addTagTypes: ["userProfile", "article"] })
  .injectEndpoints({
    endpoints: (build) => ({
      // 获取用户信息
      requestUser: build.query<UserResponse, undefined>({
        query: () => ({
          url: "/user",
        }),
      }),
      // 获取用户资料
      requestProfile: build.query<UserProfileResponse, undefined>({
        query: () => ({ url: "/user/profile" }),
        providesTags: ["userProfile"],
      }),
      // 上传头像
      upload: build.mutation<
        GeekResponse<{ id: string; photo: string }>,
        FormData
      >({
        query: (body) => ({ url: "/user/photo", method: "PATCH", body }),
        invalidatesTags: ["userProfile"],
      }),
      // 编辑用户资料
      updateProfile: build.mutation<
        GeekResponse<null>,
        Partial<Pick<User, "name" | "gender" | "birthday" | "intro">>
      >({
        query: (body) => ({ url: "/user/profile", method: "PATCH", body }),
        invalidatesTags: ["userProfile"],
      }),
      // 关注用户
      requestFollow: build.mutation<GeekResponse<{ target: string }>, string>({
        query: (id) => ({
          url: "/user/followings",
          method: "POST",
          body: { target: id },
        }),
        invalidatesTags: ["article"],
      }),
      // 取消关注用户
      requestUnFollow: build.mutation<null, string>({
        query: (id) => ({ url: `/user/followings/${id}`, method: "DELETE" }),
        invalidatesTags: ["article"],
      }),
    }),
  });

export const {
  useRequestUserQuery,
  useRequestProfileQuery,
  useUploadMutation,
  useUpdateProfileMutation,
  useRequestFollowMutation,
  useRequestUnFollowMutation,
} = userEndpoints;
